import requests
from lxml import html
from utils import get_mongo_collection

def get_bilibili_mid():
    mid=[]
    feed=get_mongo_collection('feed').find({'purpose':'B站UP主'})
    for i in feed:
        # mid.append(i['mid'])
        try:
            if i['username']:
                continue
        except KeyError:
            mid.append(i['mid'])

    return mid
def get_bilibili_username():
    mids=get_bilibili_mid()
    headers={
    'Host': 'm.bilibili.com',
    'User-Agent': 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Mobile Safari/537.36'

    }
    for mid in mids:
        req=requests.get(f'https://m.bilibili.com/space/{mid}' ,headers=headers)
        req.encoding = 'UTF-8'
        content=req.text
        html_format=html.fromstring(content)
        title = html_format.cssselect('title')
        name=title[0].text_content().split('-')[0].replace('的个人空间','')
        get_mongo_collection('feed').update_many({"mid":mid },{"$set":{'username':name}})
if __name__ == '__main__':
    get_bilibili_username()